<?php
$class = get_class($model);
$cs = Yii::app()->clientScript;
$script = "
var title = $('#".$class."_name');
title.click(function(){
    title.data('value', title.val());
});

title.blur(function(){
    var text = $(this).val();
    if (text == title.data('value')) return;
    var alias = $('#".$class."_alias').val();
    if (alias.replace(/\s+/, '').length > 0) return false;
    var url = text
            .toLowerCase() // change everything to lowercase
            .replace(/^\\s+|\\s+$/g, '') // trim leading and trailing spaces        
            .replace(/[_|\\s]+/g, '-') // change all spaces and underscores to a hyphen
            .replace(/[^a-z0-9-]+/g, '') // remove all non-alphanumeric characters except the hyphen
            .replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
            .replace(/^-+|-+$/g, '') // trim leading and trailing hyphens                
            ;
    $('#".$class."_alias').val(url);
});
";
$cs->registerScript('title-to-slug',$script, CClientScript::POS_READY);
?>

<?php if ($model->isNewRecord == false):?>
<div class="row">
    <?php echo $form->labelEx($model->currentRevision,'revision_id'); ?>
    <?php if (is_object($page)):?>
        <?php echo CHtml::encode($model->currentRevision->revision_id);?>&nbsp;&nbsp;&nbsp;
        <?php echo CHtml::link('Click here to use other revision', array('/Cms/admin/pageRevision/template', 'page_id'=>$page->id, 'revision_id'=>$this->get('page_revision',0)), array('title'=>'Click here to use other revision')); ?>
    <?php endif;?>
    <?php echo CHtml::dropDownList('revision_id', $model->currentRevision->revision_id, $revisions, (is_object($page) ? array('style'=>'display:none;')  : array())); ?>
    <?php if ($canDelete):?>
        <?php echo CHtml::link(CHtml::image(Yii::app()->theme->baseUrl.'/images/cross.gif', 'Remove this revision'), array('/Cms/admin/template/removeRevision', 'id'=>$model->id), array('title'=>'Remove this revision', 'class'=>'delete-revision')); ?>
        <?php Yii::app()->clientScript->registerScript(__CLASS__.'#DeleteRevision', "jQuery('.delete-revision').click(function(){var revision = jQuery('#revision_id').val();var url = jQuery(this).attr('href');jQuery.get(url, {revision : revision}, function(res){alert(res);window.location = '".$this->createUrl('update', array('id'=>$model->id))."';});return false;});");?>
    <?php endif;?>
</div>
<?php endif;?>

<div class="row">
    <?php echo $form->labelEx($model,'name'); ?>
    <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>100)); ?>
    <?php echo $form->error($model,'name'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'alias'); ?>
    <?php echo $form->textField($model,'alias',array('size'=>60,'maxlength'=>128)); ?>
    <?php echo $form->error($model,'alias'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model,'group'); ?>
    <?php
    $this->widget("zii.widgets.jui.CJuiAutoComplete",array(
                "model"=>$model,
                "attribute"=>"group",
                "sourceUrl"=>Yii::app()->controller->createUrl("/Core/service/ajax&FORMAT=text",array("SID"=>"Cms.Template.getGroups","eval_id"=>$this->get('eval_id'))),
                "htmlOptions"=>array("id"=>"EvaluationGoal_category", "size"=>60, "maxlength"=>255)
            ));
    ?>
    <?php echo $form->error($model,'group'); ?>
</div>
<div class="row">
  <?php echo $form->label($model,'masterLayout');?>
  <?php 
  /*if((!$model->isNewRecord) & $model->currentRevision->parent_id==0)
        $model->masterLayout=true;*/  
  ?>
  <?php echo $form->checkBox($model,'masterLayout');
  ?>
</div>
<div class="row" id="parent_drop">
    <?php echo $form->labelEx($model->currentRevision,'parent_id', array('label'=>'Parent template')); ?>
    <?php 
    array_push($childrenTemplates, $model->currentRevision->id);
    $this->widget('Cms.widgets.TemplateContentDropDownList', array(
        'model'=>$model->currentRevision,
        'attribute'=>'parent_id',
        'excludeContent'=>$childrenTemplates,
        'htmlOptions'=>array('class'=>'template-options', 'prompt'=>'---Select---'),
    ));
    ?>
    <?php echo $form->error($model->currentRevision,'parent_id'); ?>
</div>

<div class="row">
    <?php echo $form->labelEx($model->currentRevision,'html', array('label'=>'HTML')); ?>
    <?php echo $form->error($model->currentRevision,'html'); ?>
</div>

<div class="row" style="border: 1px solid black; margin-left: 210px;">
    <?php echo $form->textArea($model->currentRevision,'html',array('cols'=>70, 'rows'=>7, 'encode'=>true)); ?>
</div>
